package com.util;

import javax.imageio.ImageIO;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Hashtable;

public class ImageCompose {

    public static void main(String args[]) throws IOException {
        Image poster = ImageIO.read(new File("C:\\Users\\admin\\Desktop\\SC1.jpg"));
        //Image poster = getSrcfImage("http://tykfa.cn/hy/1596692862272_1111111111.jpg");
        int posterWidth = poster.getWidth(null);
        int posterHeight = poster.getHeight(null);

        //Image img2 = getSrcfImage("http://tykfa.cn/hy/1596185981702_%E7%8C%AB.jpg");
        Image background = ImageIO.read(new File("C:\\Users\\admin\\Desktop\\lALPDiQ3PFVGmy_NARjNArM_691_280.png"));
        int backgroundWidth = background.getWidth(null);
        int backgroundHeight = background.getHeight(null);

        Image deltaImg = ImageIO.read(new File("C:\\Users\\admin\\Desktop\\lALPD3zUOMVb3KUaDQ_13_26.png"));
        int deltaImgWidth = background.getWidth(null);
        int deltaImgHeight = background.getHeight(null);

        Image qrCode = getSrcfImage("http://tykfa.cn/hy/1596185981702_%E7%8C%AB.jpg");

        BufferedImage bufferedImage = new BufferedImage(posterWidth,posterHeight,BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics2D = bufferedImage.createGraphics();
        graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        graphics2D.drawImage(poster,0,0,posterWidth,posterHeight,null); //先画出原图

        int backgroundPositionY = posterHeight-(int)(backgroundHeight * 0.9) - 30;

        graphics2D.drawImage(background, 30,backgroundPositionY,posterWidth-60, (int) (backgroundHeight*0.9),null);

        int qrCodePositionY = posterHeight-(int)(backgroundHeight * 0.9);

        int qrCodeWidth = (int)(backgroundHeight*0.9) - 60;
        int qrCodeHeight = (int)(backgroundHeight*0.9) - 60;
        graphics2D.drawImage(qrCode,60,qrCodePositionY,qrCodeWidth,qrCodeHeight,null);

        graphics2D.drawImage(deltaImg,qrCodeWidth + 80,qrCodePositionY + 50,15,20,null);

        Graphics2D graphicText = bufferedImage.createGraphics();
        graphicText.setFont(new Font("微软雅黑",Font.BOLD,25));
        graphicText.setColor(Color.GRAY);
        graphicText.drawString("长按图片购买", qrCodeWidth + 100,qrCodePositionY + 70);
        graphicText.setFont(new Font("宋体",Font.PLAIN,20));
        graphicText.drawString("分享员：", qrCodeWidth + 100,qrCodePositionY + 110);
        graphicText.drawString("活动油站：湖北销售黄冈分公司湖北大广北", qrCodeWidth + 100,qrCodePositionY + 150);
        String str = "湖北销售黄冈分公司湖北大广北高速公路麻城罗铺东加油站";
        System.out.println(str.length());
        System.out.println(str.substring(0,14));
        System.out.println(str.substring(14,str.length()));
        graphicText.drawString("高速公路麻城罗铺东加油站", qrCodeWidth + 100,qrCodePositionY + 190);
        graphicText.dispose();
        graphics2D.dispose();
        File file = new File("C:\\Users\\admin\\Desktop\\xxxx\\");
        if(!file.exists()){
            file.mkdir();
        }
        OutputStream os = new FileOutputStream(file.getPath()+"\\asaaa.jpg");

        ImageIO.write(bufferedImage,"jpg",os);
    }

    private static Image getSrcfImage(String url) throws IOException {
        URL u = new URL(url);
        HttpURLConnection conn = (HttpURLConnection) u.openConnection();
        InputStream input = conn.getInputStream();
        //原图片对象
        Image srcImg = ImageIO.read(input);
        return srcImg;
    }
}
